package cn.lxw.objectboxsimple.ui

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import cn.lxw.objectboxsimple.R
import cn.lxw.objectboxsimple.app.App
import cn.lxw.objectboxsimple.bean.User
import cn.lxw.objectboxsimple.bean.User_

/**
 *****************************
 * 源代码：Lxw
 * EMail：China2021@126.com
 * 时间轴：2018年 04月 07日 10:23
 *****************************
 * 备注：
 *
 */
class MainActivity : Activity() {

    private var btInsert: Button? = null
    private var btUpdate: Button? = null
    private var btQuery: Button? = null
    private var btDelete: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        btInsert = findViewById(R.id.bt_insert)
        btUpdate = findViewById(R.id.bt_update)
        btQuery = findViewById(R.id.bt_query)
        btDelete = findViewById(R.id.bt_delete)

        btInsert?.setOnClickListener {
            insert()
        }

        btDelete?.setOnClickListener {
            delete()
        }

        btUpdate?.setOnClickListener {
            update()
        }

        btQuery?.setOnClickListener {
            query()
        }

    }


    private fun insert() {
        for (i in 1..50) {
            App.instance.getBoxStore().boxFor(User::class.java).put(User(name = "lxw--$i", age = i))
        }
    }

    private fun delete() {
        val oldAllUsers = App.instance.getBoxStore().boxFor(User::class.java).query().build().find()
        Log.d(TAG, "数据总条数：${oldAllUsers.size}")


        //删除ID 为 3到5 之间的数据
        val users = App.instance.getBoxStore().boxFor(User::class.java).query().between(User_.id, 3, 5).build().find()
        Log.d(TAG, "找到：${users.size} 条数据。}")
        Log.d(TAG, "准备删除")
        App.instance.getBoxStore().boxFor(User::class.java).remove(users)

        val newAllUsers = App.instance.getBoxStore().boxFor(User::class.java).query().build().find()
        Log.d(TAG, "剩余条数：${newAllUsers.size}")
    }


    //将年龄为18岁的修改为100岁
    private fun update() {
        val user = App.instance.getBoxStore().boxFor(User::class.java).query().equal(User_.age, 18).build().findUnique()
        user?.let {
            it.age = 100
            App.instance.getBoxStore().boxFor(User::class.java).put(it)
            Log.d(TAG, "年龄已更新")
        }
    }

    private fun query() {

        val equalUsers = App.instance.getBoxStore().boxFor(User::class.java).query().equal(User_.name, "lxw--36").build().find()
        equalUsers.forEach {
            Log.d(TAG, "equal精确查找：lxw--36：$it")
        }

        Log.d(TAG, "-----------------------------")

        val mMutableUsers = App.instance.getBoxStore().boxFor(User::class.java).query().between(User_.age, 30, 50).build().find()
        mMutableUsers.forEach {
            Log.d(TAG, "Between查找：年龄在30~50岁之间的数据：$it")
        }

        Log.d(TAG, "-----------------------------")


        val contains7Users = App.instance.getBoxStore().boxFor(User::class.java).query().contains(User_.name, "7").build().find()
        contains7Users.forEach {
            Log.d(TAG, "contains查找：名字中包含“7”的数据：$it")
        }

        Log.d(TAG, "-----------------------------")

        val endWith1Users = App.instance.getBoxStore().boxFor(User::class.java).query().endsWith(User_.name,"8" ).build().find()
        endWith1Users.forEach {
            Log.d(TAG, " endWith查找：年龄尾数为1的数据：$it")
        }


        Log.d(TAG, "-----------------------------")


        val ageStartWith4NameEndWith3 = App.instance.getBoxStore().boxFor(User::class.java).query().startsWith(User_.name, "l").and().endsWith(User_.name, "3").build().find()
        ageStartWith4NameEndWith3.forEach {
            Log.d(TAG, "and组合查找：年龄以 “4” 开头，名字以 “3” 结尾的数据：$it")
        }

        Log.d(TAG, "-----------------------------")


        App.instance.getBoxStore().boxFor(User::class.java).query().build().find().forEach {
            Log.d(TAG, "所有的数据：$it")
        }

    }

    companion object {
        private const val TAG = "MainActivity"
    }


}